<template>
  <div class="app-container">
    <el-row v-if="!disabled" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          @click="handleAdd"
          v-hasPermi="['technology:processDetailsBom:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['technology:processDetailsBom:edit']"
          >编辑</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['technology:processDetailsBom:remove']"
          >删除</el-button
        >
      </el-col> -->
      <!-- <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="processDetailsBomList"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="投料顺序" align="center" prop="serialNo" />
      <el-table-column label="物料编号" align="center" prop="materialNo" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column
        label="物料分类"
        align="center"
        prop="materialSortName"
      />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="规格" align="center" prop="standards" />
      <el-table-column label="型号" align="center" prop="model" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="数量" align="center" prop="amount" />
      <el-table-column label="损耗系数" align="center" prop="lossCoefficient" />
      <el-table-column
        v-if="!disabled"
        label="操作"
        fixed="right"
        align="center"
        min-width="180"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['technology:processDetailsBom:edit']"
            >编辑</el-button
          >
          <el-button
            link
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['technology:processDetailsBom:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <bomSelect
      v-model:show="show"
      :key="processId"
      :multiple="false"
      @change="handleChange"
    ></bomSelect>

    <!-- 添加或修改工艺管理详情子BOM对话框 -->
    <el-dialog
      :title="title"
      draggable
      v-model="open"
      width="700px"
      :close-on-click-modal="false"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-button type="primary" @click="handleSelectPro">选择物料</el-button>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="物料编号" prop="materialNo">
              {{ form.materialNo }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="materialName">
              {{ form.materialName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料分类" prop="materialSortName">
              {{ form.materialSortName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工厂" prop="factoryName">
              {{ form.factoryName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="标准生产数量" prop="standardProdCount">
              {{ form.standardProdCount }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              {{ form.unit }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格" prop="standards">
              {{ form.standards }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号" prop="model">
              {{ form.model }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌" prop="brand">
              {{ form.brand }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="amount">
              <el-input-number v-model="form.amount" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="损耗系数" prop="lossCoefficient">
              <el-input-number
                v-model="form.lossCoefficient"
                :min="0"
                :precision="3"
                :step="0.1"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投料顺序" prop="serialNo">
              <el-input-number v-model="form.serialNo" :min="1" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProcessDetailsBom,
  getProcessDetailsBom,
  addProcessDetailsBom,
  updateProcessDetailsBom,
  delProcessDetailsBom,
} from "@/api/technology/processDetailsBom";
import bomSelect from "./components/bomSelect.vue";
export default {
  name: "ProcessDetailsBom",
  components: {
    bomSelect,
  },
  props: {
    processId: {
      type: [Number, String],
      default: "",
    },
    processManageId: {
      type: [Number, String],
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺管理详情子BOM表格数据
      processDetailsBomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      show: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialNo: null,
        materialName: null,
        materialSort: null,
        unit: null,
        standards: null,
        model: null,
        brand: null,
        amount: null,
        lossCoefficient: null,
        orgId: null,
        deleteFlag: null,
      },
      // 表单参数
      form: {
        serialNo: 1,
      },
      // 表单校验
      rules: {
        amount: [{ required: true, message: "数量不能为空", trigger: "blur" }],
        lossCoefficient: [
          { required: true, message: "损耗系数不能为空", trigger: "blur" },
        ],
        serialNo: [
          { required: true, message: "投料顺序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    processId: {
      immediate: true,
      handler() {
        this.getList();
      },
    },
  },
  methods: {
    handleSelectPro() {
      this.show = true;
    },
    handleChange(val) {
      this.form.materialNo = val.prodNo;
      this.form.materialName = val.prodName;
      this.form.materialSort = val.prodType;
      this.form.materialSortName = val.prodTypeName;
      this.form.factoryName = val.factoryName;
      this.form.factory = val.factory;
      this.form.standards = val.packages;
      this.form.standardProdCount = val.standardProdCount;
      this.form.unit = val.unit;
      // this.form.packages = val.packages;
      this.form.model = val.model;
      this.form.brand = val.brand;
    },
    /** 查询工艺管理详情子BOM列表 */
    getList() {
      this.loading = true;
      this.queryParams.workProcedureId = this.processId;
      listProcessDetailsBom(this.queryParams).then((response) => {
        this.processDetailsBomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialNo: null,
        materialName: null,
        materialSort: null,
        unit: null,
        standards: null,
        model: null,
        brand: null,
        amount: null,
        lossCoefficient: null,
        orgId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deleteFlag: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.serialNo = 1;
      this.title = "新增BOM";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProcessDetailsBom(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑BOM";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (!this.form.materialNo) {
        this.$modal.msgError("请选择物料!");
        return;
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.workProcedureId = this.processId;
          this.form.processManageId = this.processManageId;
          if (this.form.id != null) {
            updateProcessDetailsBom(this.form).then((response) => {
              this.$modal.msgSuccess("编辑成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcessDetailsBom(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      let msg = "";
      if (row.materialNo) {
        msg = `是否确认删除物料编号为"${row.materialNo}"数据项？`;
      } else {
        msg = `是否确认删除所选数据项？`;
      }
      this.$modal
        .confirm(msg)
        .then(function () {
          return delProcessDetailsBom(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
